const express = require("express")
const Memberlevel = require("../models/memberLevel")
const MemberInfo = require("../models/meberInfo")
const Expenses = require("../models/expense")
const router = express.Router()
// 会员录入表
router.post("/member",async(req,res)=>{
    await Memberlevel.create(req.body);
    res.send("会员录入成功")
})
// 会员等级查询表
router.get("/member_level_search",async(req,res)=>{
    const result=await Memberlevel.find({},{
        _id:0,
        __v:0
    })
    res.send(result)
})
// 会员信息录入
router.post("/member_info",async(req,res)=>{
    await MemberInfo.create(req.body)
    res.send("会员信息录入成功")
})
// 会员信息查询
router.get("/member_search",async(req,res)=>{
    const result=await MemberInfo.find({},{
        _id:0,
        __v:0
    })
    res.send(result)
})
// 保存消费记录
router.post("/expense",async(req,res)=>{
    await Expenses.create(req.body)
    res.send("保存成功")
})
// 查询某个人的消费记录
router.get("/expense_search",async(req,res)=>{
    const result=await Expenses.aggregate([{
        $lookup:{
            from: 'memberinfos',
            localField: 'member_id',
            foreignField: 'member_id',
            as: 'expense_list'
        }
    },{
        $match:{
            member_id: req.query.member_id
        }
    }])
    res.send(result)
})
// 会员卡充值
router.post("/topup",async(req,res)=>{
    await MemberInfo.updateOne({member_id:req.body.member_id},{$set:req.body})
    res.send("充值成功")
})
module.exports = router